//
//  @ Project : Galaxy Star Trading System
//  @ File Name : Portfolio.h
//  @ Date : 2014/9/5
//  @ Author : 
//
//


#if !defined(_CPORTFOLIO_H)
#define _CPORTFOLIO_H

namespace GSTrader
{
	namespace TradeStrategy
	{
		class CPortfolioPosition;
		class CPortfolioAccount;
		class CStrategyBase;

		class TRADE_STRATEGY_API CPortfolio 
		{
		public:
			CPortfolio(CFramework* pFramework, CStrategyBase* pStrategy);
			virtual ~CPortfolio();

		public:
			bool HasPositionById( int nInstrumentId);
			bool HasLongPositionById( int nInstrumentId);
			bool HasShortPositionById(int nInstrumentId);

			double GetNetPosition(int nInstrumentId);
			double GetLongPosition(int nInstrumentId);
			double GetShortPosition(int nInstrumentId);

		public:
			void OnExecutionReport(CExecutionReport* pReport);
			void ProcessExecutionReport(CExecutionReport* pReport, bool bQueue);
	
			void OnPosition(CPosition* pPosition, bool bProcess, bool bQueue);
			void ProcessTrade(CExecutionReport* pReport, bool bQueue);
			void ProcessPosition(CPortfolioPosition* pPosition, bool bQueue);

			void SetParent(CPortfolio* pParent);

			CTransaction* GetTransaction(int nOrderId);
			CPortfolioPosition* GetPosition(int nInstrumentId);

			CPortfolioPosition* CreatePosition(CExecutionReport* pReport);
			CPortfolioPosition* CreatePosition(int nInstrumentId, CExecutionReport* pReport, CPosition* pPosition);

			string ToString();
			void RemoveAllPosition();
			void RemoveAllTransaction();

			void Clear();

		public:
			list<CPortfolio* > m_listChild;	
			list<CPortfolioPosition*> m_listPosition;

			map<int, CTransaction*> m_mapTransactionByOrder;
			map<int, CPortfolioPosition* > m_mapPositionByInstrument;

	
			CTradeSeries* m_pTradeSeries;
			CPortfolio* m_pParent;
			CFramework* m_pFramework;
			CStrategyBase* m_pStrategy;
			CPortfolioAccount* m_pAccount;

		public:
			int m_nID;
		};
	}
}

#endif // _CPORTFOLIO_H

